/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aal.business;

import com.aal.bean.CustomerFacadeLocal;
import com.aal.bean.OrderDetailFacadeLocal;
import com.aal.bean.OrderProductFacadeLocal;
import com.aal.entity.OrderDetail;
import com.aal.entity.OrderDetailPK;
import com.aal.entity.OrderProduct;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author quy
 */
@Stateless
public class OrderManagement implements OrderManagementLocal {
    @EJB
    private OrderDetailFacadeLocal orderDetailFacade;

    @EJB
    private OrderProductFacadeLocal orderProductFacade;
    @EJB
    private CustomerFacadeLocal customerFacade;
    
    public Long maxOrderId(){
        return orderProductFacade.getMaxOrderId();
    }
    public boolean createOrderDetail(long orderId,int productId,int quantity,double totalNetPrice,double shippingCost,double taxCost){
        try{
        OrderDetail orderdetail = new OrderDetail();
        OrderDetailPK pk = new OrderDetailPK();
        pk.setOrderID(orderId);
        pk.setProductID(productId);
        orderdetail.setOrderDetailPK(pk);
        orderdetail.setQuantity(quantity);
        orderdetail.setShippingCost(shippingCost);
        orderdetail.setTaxCost(taxCost);
        orderdetail.setTotalNetPrice(totalNetPrice);
        orderDetailFacade.create(orderdetail);
        return true;
        }
        catch(Exception ex){
            return false;
        }
    }
    public boolean editOrderStatus(long orderId, int status){
        try {
            OrderProduct orders = orderProductFacade.find(orderId);
            orders.setStatus(status);
            orderProductFacade.edit(orders); 
            return true;
             }
        catch (Exception ex) {
            return false;
        }
    }
    @Override
    public boolean placeOrder(String username, Date orderdate, double totalnetprice, double discountvalue, double shippingcost, double taxcost, double totalprice, int status, String shippingaddress, String receivername, String receiverphone, String customername, String billingaddress) {
        try {
            OrderProduct orders = new OrderProduct();
            orders.setOrderID(new Long(0));
            orders.setUsername(customerFacade.find(username));
            orders.setOrderDate(new Date());
            orders.setTotalNetPrice(totalnetprice);
            orders.setDiscountValue(discountvalue);
            orders.setShippingCost(shippingcost);
            orders.setTaxCost(taxcost);
            orders.setTotalPrice(totalprice);
            orders.setStatus(status);
            orders.setShippingAddress(shippingaddress);
            orders.setReceivername(receivername);
            orders.setReceiverPhone(receiverphone);
            orders.setCustomerName(customername);
            orders.setBillingAddress(billingaddress);
            orderProductFacade.create(orders);
        } catch (Exception ex) {
            return false;
        }
        return true;
    }

    public OrderProduct getOrderProduct(long orderId) {
        return orderProductFacade.find(orderId);
    }
    
    public List<OrderProduct> viewAll() {
        return orderProductFacade.findAll();
    }
    public List<OrderDetail> viewOrderDetail(long orderID){
        return orderDetailFacade.viewOrderDetail(orderID);
    }
    public List<OrderDetail> viewAllOrderDetail(){
        return orderDetailFacade.findAll();
    }
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
}
